package com.djq.hhc.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.djq.hhc.mapper.PostTypeMapper;
import com.djq.hhc.model.post.PostInfo;
import com.djq.hhc.model.post.PostType;
import com.djq.hhc.result.Result;
import com.djq.hhc.service.PostTypeService;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: dadd7
 * @Date: 2023/03/02/下午 9:02
 * @Description:
 */

@Service
public class PostTypeServiceImpl extends ServiceImpl<PostTypeMapper, PostType> implements PostTypeService {
    @Override
    public Result updateType(PostType postType) {
        Long id = postType.getId();
        String typeName = postType.getTypeName();
        PostType one = query().eq("type_name", typeName).ne("id", id).one();
        if (one == null) {
            boolean ok = updateById(postType);
            if (ok) {
                return Result.ok().message("更新成功");
            } else {
                return Result.fail().message("更新失败");
            }
        } else {
            return Result.fail().message("该类别名称已存在");
        }
    }

    @Override
    public Result saveType(PostType postType) {
        String typeName = postType.getTypeName();
        PostType one = query().eq("type_name", typeName).one();
        if (one == null) {
            boolean ok = save(postType);
            if (ok) {
                return Result.ok().message("添加成功");
            } else {
                return Result.fail().message("添加失败");
            }
        } else {
            return Result.fail().message("该类别名称已存在");
        }
    }
}
